package com.imodia.checkoo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsCatalogueBean;
import com.imodia.checkoo.bean.CmsPageAdBean;
import com.imodia.checkoo.bean.CmsPageAdCatalogueBean;
import com.imodia.checkoo.bean.CmsPageAdSiteBean;
import com.imodia.checkoo.service.CmsCatalogueService;
import com.imodia.checkoo.service.CmsPageAdCatalogueService;
import com.imodia.checkoo.service.CmsPageAdService;
import com.imodia.checkoo.service.CmsPageAdSiteService;
import com.imodia.checkoo.service.CmsSiteService;


@Controller
@RequestMapping("/hfmanager/page_ad/list.do")
public class CmsPageAdController {
	
	private final Log log = LogFactory.getLog(CmsPageAdController.class);
	
	private final String baseUrl = "page_ad";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";

	@Autowired
	private CmsPageAdService cmsPageAdService;
	@Autowired
	private CmsCatalogueService cmsCatalogueService;
	@Autowired
	private CmsPageAdSiteService cmsPageAdSiteService;
	@Autowired
	private CmsPageAdCatalogueService cmsPageAdCatalogueService;
	@Autowired
	private CmsSiteService cmsSiteService;
	
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsPageAdService.delete(id);
		int siteId = StringUtil.requestInt(request,"siteId");
		int catalogueId = StringUtil.requestInt(request,"catalogueId");
		
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&siteId="+siteId+"&catalogueId="+catalogueId), null);
	}
	
	
	@RequestMapping(params = "action=check")
	public ModelAndView check(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsPageAdService.checked(id);
		int siteId = StringUtil.requestInt(request,"siteId");
		int catalogueId = StringUtil.requestInt(request,"catalogueId");
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&siteId="+siteId+"&catalogueId="+catalogueId), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit( HttpServletRequest request,@ModelAttribute("userMessage")CmsPageAdBean obj,BindingResult bindingResult) {
		
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsPageAdService.update(obj);
		}else{
			x = cmsPageAdService.insert(obj);
			obj.setId(x);
		}
		
		String[] siteId = request.getParameterValues("siteId");
		if(siteId!=null){
			List<CmsPageAdSiteBean> siteArticleList = new ArrayList<CmsPageAdSiteBean>();
			Map<String,CmsPageAdSiteBean> siteArticleMap = new HashMap<String,CmsPageAdSiteBean>();
			for(String s : siteId){
				CmsPageAdSiteBean siteArticle = new CmsPageAdSiteBean();
				siteArticle.setSiteId(Integer.parseInt(s));
				siteArticle.setAdId(obj.getId());
				String key = s+"_"+obj.getId();
				if(!siteArticleMap.containsKey(key)){
					siteArticleMap.put(key, siteArticle);
					siteArticleList.add(siteArticle);
				}
				
			}
			cmsPageAdSiteService.save(siteArticleList, obj.getId());
		}
		
		String[] cataId = request.getParameterValues("cataId");
		List<CmsPageAdCatalogueBean> cataArticleList = new ArrayList<CmsPageAdCatalogueBean>();
		if(cataId!=null){
			for(String s : cataId){
				if(s==null || !s.matches("\\d+")){
					continue;
				}
				CmsPageAdCatalogueBean adCatalogue = new CmsPageAdCatalogueBean();
				adCatalogue.setCatalogueId(Integer.parseInt(s));
				adCatalogue.setAdId(obj.getId());
				int positionId = 10;
				if(request.getParameter("postionId_"+s)!=null && request.getParameter("postionId_"+s).matches("\\d+")){
					positionId = Integer.parseInt(request.getParameter("postionId_"+s));
				}
				adCatalogue.setPriority(positionId);
				
				String cataSiteId = request.getParameter("cataSiteId_"+s);
				
				if(cataSiteId!=null && cataSiteId.matches("\\d+") ){
					adCatalogue.setSiteId(Integer.parseInt(cataSiteId));
				}
				cataArticleList.add(adCatalogue);
			}
		}
		cmsPageAdCatalogueService.saveBatch(cataArticleList, obj.getId());
		
		int paramSiteId = StringUtil.requestInt(request, "paramSiteId");
		int paramCatalogueId = StringUtil.requestInt(request, "paramCatalogueId");
		
		return new ModelAndView(new RedirectView(request.getContextPath()+url_edit_redirect+"&catalogueId="+paramCatalogueId+"&siteId="+paramSiteId+"&id="+obj.getId()+"&x="+x), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();
		
		CmsPageAdBean obj = cmsPageAdService.get(id);
		m.put("info",obj );
		m.put("site",cmsSiteService.list(new SqlParam()));
		
		List<CmsCatalogueBean> catalogueList = cmsCatalogueService.get(0,4,4,0);
		m.put("catalogueList", catalogueList);
		
		SqlParam saSql = new SqlParam();
		saSql.andEqualTo("ad_id", String.valueOf(id));
		List<CmsPageAdSiteBean> saList = cmsPageAdSiteService.list(saSql);
		m.put("saList", saList);
		List<CmsPageAdCatalogueBean> acList = cmsPageAdCatalogueService.list(saSql);
		m.put("acList", acList);
		
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsPageAdBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();

		PageResult<CmsPageAdBean> pageList = cmsPageAdService.selectPaginationBySql(obj,page);
		model.put("page", pageList);
		
		model.put("siteInfo",cmsSiteService.list(new SqlParam()));
		List<CmsCatalogueBean> catalogueList = cmsCatalogueService.get(0,4,4,0);
		model.put("catalogueList", catalogueList);
		return new ModelAndView(url_list, model);
	}
	
	
}
